import "reflect-metadata";
import orm = require('typeorm');
import { Entity, Column, PrimaryColumn } from "typeorm";
import { createConnection, ConnectionOptions,ConnectionManager } from "typeorm";

/**
 * 数据实体（表）
 */
@Entity()
export class city {
    @PrimaryColumn("int", { length: 11 })
    id: number;
    @Column("char", { length: 35 })
    name: string;
    @Column("char", { length: 3 })
    countryCode: string;
    @Column("char", { length: 20 })
    district: string;
    @Column("int", { length: 11 })
    population: number;

    noColumn:string;
}

/**
 * 连接配置（MySQL）
 */
let conn: ConnectionOptions = {
    type: "mysql",
    host: "localhost",
    port: 3306,
    database: "world",
    username: "root",
    password: "root",
    entities: [
        city
    ],
    autoSchemaSync: false
};

// /**
//  * 创建数据连接
//  * @remark 通过ID查找
//  */
// createConnection(conn).then(connection => {
//     let cityRepository = connection.getRepository(city);
//     console.log("---------- City Repository ----------");
//     console.log(cityRepository);
//     console.log("-------------------------------------");

//     cityRepository.findOneById(1)
//         .then(value => {
//             console.log("---------  City by id 1 -------------");
//             console.log(value);
//             console.log("-------------------------------------");
//         }, error => console.log(error));
// }).catch(error => console.log(error.message));

// createConnection(conn).then(connection => {
//     let cityRepository = connection.getRepository(city);

//     let insertCity = new city();
//     insertCity.name = "Foshan[test]";
//     insertCity.district = "Foshan[test]";
//     insertCity.countryCode = "CHN";
//     insertCity.population = 342342;

//     cityRepository
//         .save(insertCity)
//         .then(result => {
//             console.log(`${result.name} 添加成功`)
//         }, error => console.log(error.message))

// }, error => console.log(error.message));


// /**
//  * 修改
//  */
// createConnection(conn).then(connection => {
//     let cityRepository = connection.getRepository(city);

//     cityRepository.findOneById(4082)
//         .then(queryCity => {
//             if (queryCity) {
//                 console.log("---------  City by id 4081 -------------");
//                 console.log(queryCity.name);
//                 console.log("-------------------------------------");

//                 queryCity.name = "Foshan[update]";
//                 cityRepository.save(queryCity)
//                     .then(result => {
//                         console.log("---------- update City -------------");
//                     }, error => console.log(error.message));
//             }
//         }, error => console.log(error.message));
// }).catch(error => console.log(error.message));

// /**
//  * 删除
//  */
// createConnection(conn).then(connection => {
//     let cityRepository = connection.getRepository(city);

//     cityRepository.findOneById(4083)
//         .then(queryCity => {
//             if (queryCity) {
                
//                 cityRepository.remove(queryCity)
//                     .then(result => {
//                         console.log("---------- remove City -------------");
//                     }, error => console.log(error.message));
//             }
//         }, error => console.log(error.message));
// }).catch(error => console.log(error.message));

// /**
//  * 通过sql查询
//  */
// createConnection(conn).then(connection => {
//     connection.query("SELECT * FROM world.country where IndepYear > 1993")
//         .then(value => {
//             console.log("---------  execute sql -------------");
//             console.log(value);
//             console.log("-------------------------------------");
//         }, error => console.log(error.message));
// }, error => console.log(error.message));


const manager = new ConnectionManager();

let connect = manager.create(conn);
let connectPromise = connect.connect();

connectPromise.then(connection => {
    
    return connection.query("SELECT * FROM world.country");
})
.then((value) => {
    console.log("---------  execute sql IndepYear > 1989 -------------");
    console.log(`value.length : ${value.length}`);
    console.log("-------------------------------------");
    
    console.log(value.slice(0,5));
    console.log("-------------------------------------");
    
    connect.close();    
})
.catch(error => console.log(error.message));

// console.log("-------------  end  ---------------");
